#!/usr/bin python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/8 11:06
# @Author  : hhr
# coding=utf-8
import json
import time
import pymysql
from config import db_host, db_port
import redis
from playwright.async_api import async_playwright
import datetime
import asyncio

conn = pymysql.connect(host='121.4.69.146',
                       user='root',
                       port=3306,
                       password='xhhg@2020',
                       database='big_data',
                       cursorclass=pymysql.cursors.DictCursor)
# redis_con = redis.Redis(
#     host='121.4.69.146', password='xhhg123456', port=6389, db=2, decode_responses=True)
redis_con = redis.Redis(host='127.0.0.1', password="xhhg123456", port=6379, db=2, decode_responses=True)
cur = conn.cursor()

now_date = datetime.date.today()
month = now_date.month
if month < 10:
    month = f'0{month}'
day = now_date.day
if day < 10:
    day = f'0{day}'
now_time = f'{month}{day}'


def select_sqh(now_time):
    db = pymysql.connect(host=db_host,
                         user='root',
                         port=db_port,
                         password='xhhg@2020',
                         database='big_data')
    cur = db.cursor()
    sql = f''' SELECT DISTINCT application_number FROM by_payment_information_{now_time}
    union SELECT DISTINCT application_number FROM annual_fee_information_{now_time}
    union SELECT DISTINCT application_number FROM overdue_fine_{now_time}
    # union SELECT DISTINCT application_number FROM refund_fee_{now_time}
    # union SELECT DISTINCT application_number FROM chonghong_fee_{now_time}
    '''
    cur.execute(sql)
    data = cur.fetchall()
    have_already_done = set([sqh[0].replace("ZL", '') for sqh in data])
    db.close()
    cur.close()
    return have_already_done


def create_table(now_time):
    # redis创建需查询的专利号
    create_sql_payment = f"""
        CREATE TABLE If Not Exists `big_data`.`xhhg_pantent_status_{now_time}`  (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `application_number` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `case_status` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `fm_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `fm_class` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `sq_date` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `created_at` timestamp(0) NULL DEFAULT NULL,
      `updated_at` timestamp(0) NULL DEFAULT NULL,
      `priority_reviewl` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `agency` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `is_ours_cases` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `patent_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `patent_user` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `first_agent` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `port` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      `fmr` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;"""
    cur.execute(create_sql_payment)
    conn.commit()


async def get_patent_pay(patent_pay_res_json):
    # 抓取费用信息
    for jj in patent_pay_res_json["data"]["records"]:
        sql = f"""INSERT INTO xhhg_pantent_status_{now_time} (application_number, case_status, fm_name,fm_class, sq_date, created_at, updated_at, agency, patent_user, first_agent, fmr, port) VALUES
                        ('{jj["zhuanlisqh"]}', '{jj["anjianywzt"]}', '{jj["zhuanlimc"]}', '{jj['zhufenlh']}', '{jj["shenqingr"]}', NOW(), NOW(), '', '{jj["shenqingrxm"]}', '', '', '{jj["gongkaiggr"]}')"""
        try:
            cur = conn.cursor()
            cur.execute(sql)
            conn.commit()
        except:
            time.sleep(5)
            cur = conn.cursor()
            cur.execute(sql)
            conn.commit()

async def run(inst, port, sem):
    all_start_time = time.time()
    async with sem:
        storage_state_path = f'./auth/{inst}_state.json'
        with open(storage_state_path) as file:
            storage_state = json.load(file)
            origins = storage_state.get('origins')
            local_stroge_list = [item.get('localStorage') for item in origins]
            for local_stroge in local_stroge_list:
                for item in local_stroge:
                    if item.get('name') == 'ACCESS_TOKEN':
                        authorization = item.get('value')
        print(f'成功读取{inst}登录信息:{authorization}')
        async with async_playwright() as p:
            browser = await p.firefox.launch()
            context = await browser.new_context(base_url='https://cpquery.cponline.cnipa.gov.cn', storage_state=storage_state_path, ignore_https_errors=True)
            context.set_default_timeout(300000)
            context.set_default_navigation_timeout(300000)
            page = await context.new_page()
            page.set_default_timeout(300000)
            page.set_default_navigation_timeout(300000)
            await page.goto('https://cpquery.cponline.cnipa.gov.cn/chinesepatent/index')
            # page.wait_for_load_state('networkidle')
            # while redis_con.scard(port):
            for num in range(1, 2):
                # sqh = redis_con.spop(port).replace("ZL", '')
                start_time = time.time()
                print(f'@@开始查询第:{num}页专利信息@@')
                api_request_context = context.request
                headers = {
                    "Accept": "application/json, text/plain, */*",
                    "Content-Type": "application/json;charset=utf-8",
                    "Authorization": 'Bearer ' + authorization,
                    "userType": "USER_RZ_DAILIJIG"
                }
                # 普通查询
                # json_data = {'page': num, 'shenqingrxm': "武汉儿童医院", "size": 500, "sortDataName": "", "sortType": "", "zhuanlilx": ""}
                # 代理机构查询
                json_data = {'page': num, 'searchType': 'dljg', 'shenqingrxm': "武汉儿童医院", "size": 500, "sortDataName": "", "sortType": "", "zhuanlilx": ""}
                # 申请人查询
                # json_data = {'page': num, 'searchType': 'sqr', 'shenqingrxm': "武汉儿童医院", "size": 500, "sortDataName": "", "sortType": "", "zhuanlilx": ""}
                try:
                    # 普通查询
                    # fyxx_response = await api_request_context.post('https://cpquery.cponline.cnipa.gov.cn/api/search/undomestic/publicSearch', data=json.dumps(json_data), headers=headers, timeout=120000)
                    # 代理机构查询
                    fyxx_response = await api_request_context.post('https://cpquery.cponline.cnipa.gov.cn/api/search/domestic/certifiedSearch', data=json.dumps(json_data), headers=headers, timeout=120000)

                    patent_pay_res_json = await fyxx_response.json()
                    await get_patent_pay(patent_pay_res_json)
                    # end_time = time.time()
                    # print(f"{sqh}_本次耗时: {end_time-start_time}")
                    # need_time = round((end_time - start_time) *
                    #                   redis_con.scard(port) / 60, 1)
                    # print(
                    #     f"剩余{redis_con.scard(port)}个，大约还需要{need_time}分钟")
                except Exception as E:
                    # redis_con.sadd(port, sqh)
                    # print(f'{sqh}_查询超时，重试', E)
                    continue
                await page.reload()

    all_end_time = time.time()
    print(f"@@总耗时：{all_end_time-all_start_time}@@")


async def main(inst, port, max_process):
    sem = asyncio.Semaphore(max_process)  # 限制并发量
    task_list = [asyncio.create_task(
        run(inst, port, sem))for _ in range(max_process)]
    await asyncio.gather(*task_list)

if __name__ == '__main__':
    print(f'now_time:{now_time}')
    create_table(now_time=now_time)
    print('@@成功创建目标表@@')
    # have_already_done = select_sqh(now_time)
    print('@@成功获取已抓取数据@@')
    port_dict = {'XHHG': '外部接口查询', 'XH': '协和端口查询', 'WB': "外部接口查询"}
    inst = 'XHHG'
    port = port_dict.get(inst)
    # 异步
    max_process = 5  # 最大并发数
    asyncio.run(main(inst, port, max_process))
    conn.close()
